<?php

require_once('functions/pageLoad.php');
loginRequired(true);
$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['jid']) && isset($_POST['rating']) && isset($_POST['review'])){

$sql = "SELECT jobs.id, tradesman_id, area, trade, quote FROM jobs 
	LEFT JOIN quotes ON jobs.quote_id = quotes.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE jobs.user_id = '".$user_id."' AND jobs.id = '".mysql_real_escape_string($_POST['jid'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$job_rows = mysql_num_rows($query);
$job_rs=mysql_fetch_assoc($query);

if($job_rows > 0){

$sql = "INSERT INTO reviews (user_id, job_id, tradesman_id, rating, review, date_reviewed) VALUES ('".$user_id."', '".mysql_real_escape_string($_POST['jid'])."', '".$job_rs['tradesman_id']."', '".mysql_real_escape_string($_POST['rating'])."', '".mysql_real_escape_string(stripslashes($_POST['review']))."', NOW())";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

// send email to tradesman

$sql = "SELECT username, email, firstname, surname, balance, lucky FROM users 
	LEFT JOIN tradesman ON users.id = tradesman.user_id 
	WHERE tradesman.id = '".$job_rs['tradesman_id']."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$user_rows = mysql_num_rows($query);
$user_rs=mysql_fetch_assoc($query);

$tradesman_link = build_tradesman_link($user_rs['username'],$job_rs['tradesman_id']);

require_once("classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = "mail@iwantatradesman.co.uk";
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($user_rs['email']);

	$mail->Subject = "Feedback notification";
	$mail->Body = "Dear ".$user_rs['firstname']." ".$user_rs['surname']."\r\n\r\nA user has left you feedback for a job you have completed.\r\n\r\nYou can view the feedback on your company profile here: http://www.iwantatradesman.co.uk".$tradesman_link."\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

if($user_rs['lucky'] != '1'){

$new_balance = ($job_rs['quote'] * ($settings['commission'] / 100));
$new_balance = ($new_balance < $settings['minimum_commission']) ? $settings['minimum_commission'] + $user_rs['balance'] : $new_balance + $user_rs['balance'];

$sql = "UPDATE tradesman SET balance = '".$new_balance."' WHERE id = '".$job_rs['tradesman_id']."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

}

header("Location: /my-account");

}

}

?>